home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 816 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Problem with stringcopy
  5. Date: Tue, 09 Jan 96 13:57:59 GMT
  6. Organization: none
  7. Message-ID: <821195879snz@genesis.demon.co.uk>
  8. References: <4clguu$9fs@eagle.novo.dk> <820933963snz@genesis.demon.co.uk> <4cq9dr$if9@ns.RezoNet.NET> <4cqppv$1quo@news.gate.net>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4cqppv$1quo@news.gate.net> bhutto@gate.net "William Hutto" writes:
  15.  
  16. >In article <4cq9dr$if9@ns.RezoNet.NET>,
  17. >   ray@ultimate-tech.com (Ray Dunn) wrote:
  18. >>In referenced article, Lawrence Kirby says...
  19. >>>Morten Brun writes:
  20. >>>
  21. >>>>How do i do a partial stringcopy i.e. copy from a specific position 
  22. >>>>in a string and a certain numbers of bytes. I am looking for a 
  23. >>>>function like target = Stringcopy(source, startpos, length)
  24. >>>
  25. >>>target[0] = '\0';
  26. >>>strncat(target, source+startpos, length);
  27. >>
  28. >>....but use strncpy if you are overwriting some existing characters in 
  29. >>target and don't want the target string to terminate after the copied 
  30. >>characters.
  31.  
  32. memcpy is more commonly used in situations like this (when you are dealing
  33. with possibly non null-terminated sequences of characters you usually
  34. have the length available separately).
  35.  
  36. >Considering strncpy()'s shortcomings, I amend my code:
  37. >
  38. >char destination[ > max_length];
  39. >
  40. >        strncpy(destination,&source[startpos],max_length);
  41. >        destination[max_length]='\0';
  42.  
  43. But once you've got to that you're probably better off with the strncat
  44. version unless you explicitly want the destination filled with trailing
  45. null characters all the way up to destination+max_length.
  46.  
  47. strncpy works well when used for its intended purpose (writing to a
  48. fixed width array where 'strings' shorter than the array size are padded
  49. to the full length with null characters). Once such a 'semi-string' is
  50. created it is a fixed width datastructure and is naturally copied around
  51. with memcpy, compared with memcmp etc. C also directly supports outputting
  52. such datastructures with printf("%.*s", width, semistring);
  53.  
  54. strncpy is rarely useful for anything else.
  55.  
  56. -- 
  57. -----------------------------------------
  58. Lawrence Kirby | fred@genesis.demon.co.uk
  59. Wilts, England | 70734.126@compuserve.com
  60. -----------------------------------------
  61.